﻿using System;
using System.Collections;
using System.Data;
using viNews.Providers;
using visinia.SmartEngine.Cache;
using visinia.SmartEngine.Installer;

namespace viNews.Controllers
{
    /// <summary>
    /// BaseController controls the viNews main settings
    /// </summary>
    public class BaseController
    {
        public const string KEY_BASE_SETTINGS = "viNewsBaseSettings";

        /// <summary>
        /// The url separator for the viNews plugin
        /// </summary>
        public static string viNewsUrlSeparator
        {
            get { return PluginController.GetInstalledPlugin("viNews").UrlSeparator; }
        }

        /// <summary>
        /// The name of the viNews front page used internally
        /// </summary>
        public static string FrontPageName
        {
            get { return "viNewsFrontPage"; }
        }

        /// <summary>
        /// The name of the viNews content page used internally
        /// </summary>
        public static string ContentPageName
        {
            get { return "viNewsContentPage"; }
        }
        
        /// <summary>
        /// The viNews front page id
        /// </summary>
        public static int FrontPageId
        {
            get { return Convert.ToInt32(Settings["FrontPageId"]); }
            set { AddSetting("FrontPageId", value.ToString()); }
        }

        /// <summary>
        /// The viNews content page id
        /// </summary>
        public static int ContentPageId
        {
            get { return Convert.ToInt32(Settings["ContentPageId"]); }
            set { AddSetting("ContentPageId", value.ToString()); }
        }

        /// <summary>
        /// Wether to display comment after moderation
        /// </summary>
        public static bool ModerateComments
        {
            get { return GetValue(); }
            set { AddSetting("ModerateComments", value.ToString()); }
        }

        private static bool GetValue()
        {
            if (Settings["ModerateComments"].ToString().ToLower() == "true")
                return true;
            else
                return false;
        }

        /// <summary>
        /// Gets the setting
        /// </summary>
        /// <param name="key">The settings key to get</param>
        /// <returns>returns the setting value</returns>
        public static string GetSetting(string key)
        {
            if (Settings.Contains(key))
                return (string)Settings[key];
            else
                return string.Empty;
        }

        private static Hashtable _Settings;
        private static Hashtable Settings
        {
            get
            {
                _Settings = (Hashtable)DataCache.GetCache(KEY_BASE_SETTINGS);
                if (_Settings == null)
                {
                    _Settings = new Hashtable();
                    IDataReader dr = viNewsDataProvider.Provider.GetBaseSettings();
                    while (dr.Read())
                    {
                        if (dr["SettingName"] != Convert.DBNull)
                        {
                            if (dr["SettingValue"] != Convert.DBNull)
                                _Settings[(string)dr["SettingName"]] = (string)dr["SettingValue"];
                            else
                                _Settings[(string)dr["SettingName"]] = "";
                        }
                    }
                    dr.Close();
                    DataCache.SetCache(KEY_BASE_SETTINGS, _Settings);
                }
                return _Settings;
            }
        }

        #region DATA
        /// <summary>
        /// Adds a new viNews setting Or updates it if it already exists
        /// </summary>
        /// <param name="key">The key to setting</param>
        /// <param name="value">The value of the setting</param>
        public static void AddSetting(string key, string value)
        {
            if (Settings.ContainsKey(key))
                viNewsDataProvider.Provider.UpdateBaseSetting(key, value);
            else
                viNewsDataProvider.Provider.AddBaseSetting(key, value);
            DataCache.DeleteCacheMultiple("viNews");
        }
        /// <summary>
        /// Deletes the viNews setting
        /// </summary>
        /// <param name="key">The key to delete</param>
        public static void DeleteSetting(string key)
        {
            viNewsDataProvider.Provider.DeleteBaseSetting(key);
            DataCache.DeleteCacheMultiple("viNews");
        }
        #endregion
    }
}
